package com.itbupt.eat.controller;

import com.itbupt.eat.common.R;
import com.itbupt.eat.entity.ShoppingCart;
import com.itbupt.eat.service.impl.ShoppingCartServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/5/2 0:59
 * ClassName: ShoppingCartController
 * Description:
 */

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartServiceImpl shoppingCartService;


    /**
     * 添加菜品到购物车
     * @param shoppingCart  购物车对象
     * @return  购物车对象
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        return R.success(shoppingCartService.add(shoppingCart));
    }


    /**
     * 查询当前用户的购物车中所有信息
     * @return 购物车信息
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        return R.success(shoppingCartService.getUserList());
    }


    /**
     * 清空购物车
     * @return  是否清空成功
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        if (shoppingCartService.clean()) {
            return R.success("清空成功");
        }
        return R.error("清空失败");
    }


    /**
     * 购物车商品减一
     * @param shoppingCart  购物车对象
     * @return  是否减一成功
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        if (shoppingCartService.sub(shoppingCart)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }



}
